﻿@{
    ViewBag.Title = "角色选择";
    Layout = "~/Views/Shared/_DialogLayout.cshtml";
}

<div class="dialog-content">
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })

    <div class="form-horizontal">
        <ul class="wysihtml5-toolbar">
            <li class="toolbar-right">
                <div class="input-append input-icon-prepend">
                    <div class="add-on">
                        <a title="search" class="icon"><i class="icofont-search"></i></a>
                        <input type="text" id="searchkey" data-fieldname="searchkey" placeholder="输入角色名称查询">
                    </div>
                    <button id="btnquery" class="btn btn-small">查询</button>
                </div>
            </li>
        </ul>
    </div>
    <div data-autosize="true" style="height:320px;">
        <table id="datagrid" data-options="fit:true"></table>
    </div>
</div>
<div class="dialog-footer">
    <button id="btnclose" class="btn btn-small btn-default">关闭</button>
    <button id="btnok" class="btn btn-small btn-primary">确定</button>

</div>
@section scripts{
    <script type="text/javascript">

        //回调函数
        var callback = "@ViewBag.CallBack";
        //是否单选
        var singleselect = @(ViewBag.SingleSelect ? "true" : "false");

        var custcode = "@ViewBag.custcode";

        //列定义
        var cols = [[
            { field: "Id", title: 'Id', width: 10, hidden: true },
            { field: "ck", title: '', width: 20, checkbox: true },
            { field: "Roletype", title: '角色类别', sortable: true, width: 100, align: 'left' },
            { field: "Roleid", title: '角色代码', sortable: true, width: 100, align: 'left' },
            { field: "Rolename", title: '角色名称', sortable: true, width: 100, align: 'left' },
            { field: "Description", title: '角色描述', sortable: true, width: 200, align: 'left' }
        ]];

        $(document).ready(function () {
            //关闭
            $("#btnclose").click(function (evt) { evt.preventDefault(); parent.closeDialog(); return false; });

            //查询
            $("#btnquery").click(function (evt) {
                evt.preventDefault();
                var params = $("#datagrid").datagrid('options').queryParams;
                params.custcode=custcode;
                params.s = $("#searchkey").val();
                $("#datagrid").datagrid("load");
                $("#datagrid").datagrid("clearSelections");
            });

            $("#searchkey").keydown(function (event) {
                if (event.keyCode == $.ui.keyCode.ENTER) {
                    event.preventDefault();
                    $("#btnquery").click();
                    return false;
                }
            });
            //确认
            $("#btnok").click(function (evt) {
                evt.preventDefault();
                var jsondata = [];

                var selectedRows = $("#datagrid").datagrid("getSelections");

                if(selectedRows.length==0){
                    $.messager.alert(csms_lang.message_info_title,csms_lang.message_please_select_row);
                    return;
                }

                if(singleselect){
                    parent.eval(callback + "('" + JSON.stringify(selectedRows[0]) + "')");
                }else{
                    parent.eval(callback + "('" + JSON.stringify(selectedRows) + "')");
                }

                parent.closeDialog();
            });

            LoadDatagrid();
        });

        //加载列表数据
        function LoadDatagrid() {
            var $dg = $("#datagrid");
            $dg.datagrid({
                //title: '子项目字典',
                url: baseurl + 'Public/RoleListJson',
                fit: true,
                fitColumns: true,
                striped: true,
                nowrap: true,
                idField: 'Id',
                pagination: true,
                pageNumber: 1,
                pageSize: 15,
                pageList: [10,15, 20, 30, 50, 80, 100],
                rownumbers: true,
                singleSelect: singleselect,
                queryParams: {
                    custcode:custcode,
                    s: ""
                },
                sortName: 'Roleid',
                sortOrder: 'asc',
                columns: cols,
                onLoadSuccess: function () {
                    //多选时，加载完成后清除所有选中项（不然可能会有残留选中项）
                    //$dg.datagrid("clearSelections");
                },
                onDblClickRow: function (rowIndex, row) {
                    $("#btnok").click();
                },
                //onSelect: function (rowIndex, row) {
                //    var selectedRows = $dg.datagrid("getSelections");
                //    if (selectedRows.length == 0) {
                //        $("#btnok").attr("disabled", true);
                //    } else {
                //        $("#btnok").attr("disabled", false);
                //    }
                //},
                //onUnselect: function (rowIndex, row) {
                //    var selectedRows = $dg.datagrid("getSelections");
                //    if (selectedRows.length == 0) {
                //        $("#btnok").attr("disabled", true);
                //    } else {
                //        $("#btnok").attr("disabled", false);
                //    }
                //}
            });
            //PaginationInit();
        }
    </script>
}